#include "TestCheckBox.h"
#include<QCheckBox>
#include<QDebug>
#include<QVBoxLayout>
#include<QGroupBox>

TestCheckBox::TestCheckBox(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *vlayout =new QVBoxLayout(this);
    //1.设置开关
    //1.1 记住密码
    QCheckBox *rememberPassword = new QCheckBox("记住密码");
    vlayout->addWidget(rememberPassword);

    //1.2启用功能
    QCheckBox *checkBox = new QCheckBox("启用功能", this);
    checkBox->setChecked(true); // 默认选中
    vlayout->addWidget(checkBox);

    // 监听状态变化
    connect(checkBox, &QCheckBox::stateChanged, [](int state) {
        if (state == Qt::Checked) {
            qDebug() << "已选中";
        } else if (state == Qt::Unchecked) {
            qDebug() << "未选中";
        } else if (state == Qt::PartiallyChecked) {
            qDebug() << "部分选中";
        }
    });

    // 2: 多个复选框 - 选项选择
    QGroupBox *multiCheckBoxGroup = new QGroupBox("选择兴趣", this);
    vlayout->addWidget(multiCheckBoxGroup);
    QVBoxLayout *multiLayout = new QVBoxLayout(multiCheckBoxGroup);

    QCheckBox *sports = new QCheckBox("运动", this);
    QCheckBox *music = new QCheckBox("音乐", this);
    QCheckBox *reading = new QCheckBox("阅读", this);
    QCheckBox *travel = new QCheckBox("旅行", this);

    multiLayout->addWidget(sports);
    multiLayout->addWidget(music);
    multiLayout->addWidget(reading);
    multiLayout->addWidget(travel);


    //3: 三态复选框 - 全选功能
    QGroupBox *tristateGroup = new QGroupBox("文件选择", this);
    vlayout->addWidget(tristateGroup);
    QVBoxLayout *tristateLayout = new QVBoxLayout(tristateGroup);

    QCheckBox *selectAll = new QCheckBox("全选", this);
    selectAll->setTristate(true);

    QCheckBox *file1 = new QCheckBox("文件1.pdf", this);
    QCheckBox *file2 = new QCheckBox("文件2.docx", this);
    QCheckBox *file3 = new QCheckBox("文件3.xlsx", this);

    tristateLayout->addWidget(selectAll);
    tristateLayout->addWidget(file1);
    tristateLayout->addWidget(file2);
    tristateLayout->addWidget(file3);
    // 连接三态复选框的信号
    connect(selectAll, &QCheckBox::stateChanged, this,[=](){
        Qt::CheckState state =selectAll->checkState();
        if(state == Qt::CheckState::Unchecked )
        {
                // 获取所有子控件
                const QList<QWidget*> childWidgets = tristateGroup->findChildren<QWidget*>();
                for (QWidget *widget : childWidgets) {
                    if (QCheckBox *checkBox = qobject_cast<QCheckBox*>(widget)) {
                        qDebug() << "复选框:" << checkBox->text();
                        if(checkBox->isChecked() ){
                            checkBox->setCheckState(Qt::CheckState::Unchecked);
                        }
                    }
                }
        }
        else if(state == Qt::CheckState::Checked)
        {
            // 获取所有子控件
            const QList<QWidget*> childWidgets = tristateGroup->findChildren<QWidget*>();
            for (QWidget *widget : childWidgets) {
                if (QCheckBox *checkBox = qobject_cast<QCheckBox*>(widget)) {
                    qDebug() << "复选框:" << checkBox->text();
                    if(!checkBox->isChecked() ){
                        checkBox->setCheckState(Qt::CheckState::Checked);
                    }
                }
            }
        }
    });

    //4: 依赖复选框 - 启用/禁用其他控件
    QGroupBox *dependantGroup = new QGroupBox("依赖复选框示例: 高级设置", this);
    vlayout->addWidget(dependantGroup);
    QVBoxLayout *dependantLayout = new QVBoxLayout(dependantGroup);

    QCheckBox *enableAdvanced = new QCheckBox("启用高级设置", this);
    QCheckBox *advancedOption1 = new QCheckBox("高级选项1", this);
    QCheckBox *advancedOption2 = new QCheckBox("高级选项2", this);

    // 初始状态下禁用高级选项
    advancedOption1->setEnabled(false);
    advancedOption2->setEnabled(false);

    dependantLayout->addWidget(enableAdvanced);
    dependantLayout->addWidget(advancedOption1);
    dependantLayout->addWidget(advancedOption2);
    connect(enableAdvanced, &QCheckBox::stateChanged, this, [=](int state) {
                bool enabled = (state == Qt::Checked);
                advancedOption1->setEnabled(enabled);
                advancedOption2->setEnabled(enabled);
            });

}
